package com.src.myapplication.ui.view.log;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.src.myapplication.MainActivity;
import com.src.myapplication.R;
import com.src.myapplication.ui.view.log.presenter.LoginPresenter;
import com.src.myapplication.ui.view.log.view.ILoginView;

public class LogActivity extends AppCompatActivity  implements View.OnClickListener, ILoginView {
    private EditText etName, etPwd;
    private ProgressBar mBar;
    private LoginPresenter mPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);

        initView();

        //实例化Presenter
        mPresenter = new LoginPresenter(this);
    }

    private void initView() {
        etName = (EditText) findViewById(R.id.et_name);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        mBar = (ProgressBar) findViewById(R.id.pb_progress);
        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.btn_reset).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login://发送验证吗
                mPresenter.login();

                break;
            case R.id.btn_reset://登录
                mPresenter.yanzheng();
                break;
        }
    }

    @Override
    public String getUserName() {

        return etName.getText().toString().trim();
    }

    @Override
    public String getPassWord() {

        return etPwd.getText().toString().trim();
    }

    @Override
    public void resetMsg() {
        etName.setText("");
        etPwd.setText("");
    }

    @Override
    public void showLoginProgress() {
        mBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoginProgress() {
        mBar.setVisibility(View.GONE);
    }

    @Override
    public void toSucceedActivity() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void showLoginFailedView() {
        startActivity(new Intent(this, MainActivity.class));
        Toast.makeText(this, "登录失败...", Toast.LENGTH_SHORT).show();
    }

    @Override
    public Context getcontext() {
        return LogActivity.this;
    }
}

